from datetime import timedelta
from typing import Optional

from redis.asyncio import Redis

from src.models import CustomModel

redis_client: Redis = None  # type: ignore


class RedisData(CustomModel):
    key: bytes | str
    value: bytes | str
    ttl: Optional[int | timedelta] = None


async def set_redis_key(redis_data: RedisData, *, is_transaction: bool = False) -> None:
    with redis_client.pipeline(transaction=is_transaction) as pipe:
        pipe.set(redis_data.key, redis_data.value)
        if redis_data.ttl:
            pipe.expire(redis_data.key, redis_data.ttl)

        pipe.execute()


async def get_by_key(key: str):
    result = redis_client.get(key)
    if result is not None:
        return result
    else:
        return None


async def delete_by_key(key: str) -> None:
    return await redis_client.delete(key)
